package com.flyme.module.base.provider.apiPc;


import cn.hutool.core.convert.Convert;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.common.core.utils.ApiAssert;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.entity.SysCollecon;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.provider.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;


/**
 * @author flyme
 */
@Slf4j
@Api(tags = "pc个人中心接口")
@RestController
@RequestMapping("/pcUser")
public class PcApiUserController extends BaseController<SysUserService, SysUser> {

    @Autowired
    private SysHistoryService historyService;
    @Autowired
    private SysColleconService colleconService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysEducationService educationService;
    @Autowired
    private SysMessageService messageService;
    @Autowired
    private SysVipService vipService;

    /**yes
     * 记录页面
     */
    @PostMapping(value = "/recordListWeb")
    public ResultBody recordListWeb(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo){
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        EntityMap data = new EntityMap();
        //浏览记录
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("h.id historyId,c.isVipFree,c.courseImage,c.coursePrice,c.courseTitle,c.id courseId,(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) cataNum,l.lecturerName");
        cq.eq("h.userId", userAuth.getUserId());
        cq.orderByDesc("h.createTime");
        cq.createAlias("course", "c");
        cq.createAlias("lecturer", "l");
        PqGrid pqGrid = historyService.pageList(cq);
        data.put("history", pqGrid);
        data.put("isVip", 0);
        OpenUserAuth openUserAuth = OpenHelper.getUserAuth();
        if(ObjectUtils.isNotEmpty(openUserAuth)){
            Integer vip = vipService.isVip(openUserAuth.getUserId());
            data.put("isVip", vip);
        }
        return ResultBody.success(data);
    }

    /**yes
     * 批量删除浏览记录
     */
    @PostMapping(value = "/delHistoryWeb")
    public ResultBody delHistoryWeb(HttpServletResponse response,String ids){
        ApiAssert.notNull("参数不能为空", ids);
        String[] idList = ids.split(",");
        for(String id : idList){
            boolean flag = historyService.removeById(id);
        }
        return ResultBody.ok("删除成功");
    }

    /**yes
     * 我的收藏
     */
    @PostMapping(value = "/userColleconWeb")
    public ResultBody userColleconWeb(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo){
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        CriteriaQuery cq = new CriteriaQuery<SysCollecon>(pagerInfo);
        cq.select("c.targetType,c.id colleconId,c.targetId,cs.isVipFree,cs.onLine,"
                + "CASE WHEN ISNULL(cs.courseImage) THEN live.liveImage ELSE cs.courseImage END img,"
                + "CASE WHEN ISNULL(cs.courseTitle) THEN live.liveTitle ELSE cs.courseTitle END title,"
                + "CASE WHEN ISNULL(cs.coursePrice) THEN live.regFee ELSE cs.coursePrice END price,"
                + "(select count(id) from sys_catalog ca where ca.courseId=c.targetId and parentId!=0) cataNum,"
                + "CASE WHEN ISNULL(l.lecturerName) THEN le.lecturerName ELSE l.lecturerName END lecturerName");
        cq.eq("userId", userAuth.getUserId());
        cq.eq("optType", 1);
        cq.orderByDesc("c.createTime");
        cq.createAlias("course", "cs");
        cq.createAlias("lecturer", "l");
        cq.createAlias("live", "live");
        cq.createAlias("lecturer2", "le");
        PqGrid pqGrid = colleconService.pageList(cq);
        //是否是vip用户
        EntityMap map = new EntityMap();
        map.put("isVip", 0);
        OpenUserAuth openUserAuth = OpenHelper.getUserAuth();
        if(ObjectUtils.isNotEmpty(openUserAuth)){
            Integer vip = vipService.isVip(openUserAuth.getUserId());
            map.put("isVip", vip);
        }
        map.put("shoucang",pqGrid);
        return ResultBody.success(map);
    }

    /**yes
     * 用户个人资料页面
     */
    @PostMapping(value = "/userDataWeb")
    public ResultBody userDataWeb(){
        EntityMap map = new EntityMap();
        OpenUserAuth openUserAuth = OpenHelper.getUserAuth();
        SysUser accountInfo = userService.getById(openUserAuth.getUserId());
        map.put("userHead", accountInfo.getAvatar());
        map.put("userName", accountInfo.getUserName());//姓名
        map.put("nickName", Convert.unicodeToStr(accountInfo.getNickName()));//昵称
        map.put("sex", accountInfo.getSex());//性别
        map.put("birthday", accountInfo.getBirthday());//生日
        map.put("company", accountInfo.getCompany());//公司
        map.put("department", accountInfo.getDeptName());//部门
        map.put("positionName", accountInfo.getPositionName());//职位
        map.put("telephone", accountInfo.getMobile());//电话
        map.put("email", accountInfo.getEmail());//邮箱
        return ResultBody.success(map);
    }


}
